package com.android.questions.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import androidx.viewbinding.ViewBinding;

import com.android.base.ui.base.VBBaseActivity;
import com.android.base.ui.dialog.DialogCustom;
import com.android.base.util.DQLog;
import com.android.base.util.ToastUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.config.AppDataSourse;
import com.android.questions.config.LoginConfig;
import com.android.questions.databinding.ActivitySettingBinding;
import com.android.questions.litepal.AnswerLitePalEntity;
import com.android.questions.ui.adapter.MeAdapter;
import com.android.questions.util.sound.MediaBackManager;
import com.bumptech.glide.Glide;
import com.hx.currency.HXCurrencySDK;
import com.hx.currency.data.api.BaseResp;
import com.hx.update.utils.UpdateUtil;
import com.qq.e.o.data.HttpUtil;
import com.qq.e.o.utils.JsonUtil;

import org.litepal.LitePal;

/**
 * FileName: SettingActivity
 * Author: admin
 * Date: 2021/11/8 10:56
 * Description:
 */
public class SettingActivity extends VBBaseActivity implements View.OnClickListener {

    private ActivitySettingBinding settingBinding;
    private MeAdapter meAdapter1;
    private MeAdapter meAdapter2;
    private SettingActivity activity;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, SettingActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected ViewBinding getViewBinding() {
        setTitleBackground(BG_WHITE);
        return settingBinding = ActivitySettingBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initView() {
        super.initView();
        activity = this;
        settingBinding.tvTitle.setText("设置");

        String userId = LoginConfig.getUserId();
        if (!TextUtils.isEmpty(userId)) {
            settingBinding.tvUserId.setText("ID:" + HXCurrencySDK.getDecryptUserId(userId));
        }

        Glide.with(activity)
                .load(LoginConfig.getUserHeadUrl())
                .error(R.drawable.icon_user_default_header)
                .placeholder(R.drawable.icon_user_default_header)
                .into(settingBinding.imgUserPic);

        settingBinding.ssEffect.setChecked(AppConfig.getGameSound());
        settingBinding.ssMusic.setChecked(AppConfig.getBackMusic());

        meAdapter1 = new MeAdapter();
        meAdapter2 = new MeAdapter();
        meAdapter1.setList(AppDataSourse.getSettingData1());
        meAdapter2.setList(AppDataSourse.getSettingData2());
        settingBinding.recyclerView1.setAdapter(meAdapter1);
        settingBinding.recyclerView2.setAdapter(meAdapter2);
    }

    @Override
    public void initLister() {
        super.initLister();
        settingBinding.imgBack.setOnClickListener(this);

        settingBinding.ssEffect.setOnCheckedChangeListener((buttonView, isChecked) -> AppConfig.setGameSound(isChecked));

        settingBinding.ssMusic.setOnCheckedChangeListener((buttonView, isChecked) -> {
            AppConfig.setBackMusic(isChecked);
            if (!isChecked) {
                MediaBackManager.getInstance(activity).pause();
            } else {
                MediaBackManager.getInstance(activity).start();
            }
        });

        meAdapter1.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:
                    UserServiceAgreementActivity.startAct(activity, UserServiceAgreementActivity.TYPE_PRIVACY);
                    break;
                case 1:
                    UserServiceAgreementActivity.startAct(activity, UserServiceAgreementActivity.TYPE_USER);
                    break;
            }
        });

        meAdapter2.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:
                    HelpActivity.startActivity(activity);
                    break;
                case 1:
                    AboutActivity.startActivity(activity);
                    break;
                case 2:
//                    ToastUtil.showShortToast(activity, "当前已是最新版本");
                    if (AppConfig.updateInfo != null) {
                        UpdateUtil.checkUpdate(activity, AppConfig.updateInfo, true);
                    }
                    break;
            }
        });

        settingBinding.tvCancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DialogCustom(activity)
                        .setTitle("温馨提示")
                        .setMsg("注销账号，当前账户所有数据会被永久清除，且后续无法恢复，注销前请再三确认")
                        .setLeft("取消")
                        .setRight("确认")
                        .setActionLister(new DialogCustom.ActionLister() {
                            @Override
                            public void onLeftClick() {
                            }

                            @Override
                            public void onRightClick() {
                                cancellationAccount();
                            }
                        }).show();
            }
        });
    }

    private void cancellationAccount() {
        HXCurrencySDK.getInstance().qaLogoutReq(new HttpUtil.HttpUtilCallback() {
            @Override
            public void onSuccess(int i, String response) {
                DQLog.e("注销成功：" + response);
                BaseResp baseResp = JsonUtil.parseObject(response, BaseResp.class);
                if (baseResp != null) {
                    if (baseResp.getErrorCode() == 0) {
                        LoginConfig.logout();
                        HXCurrencySDK.getInstance().clearUserId();
                        LitePal.deleteAll(AnswerLitePalEntity.class);
                        ToastUtil.showShortToast(activity, "注销成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                activity.finish();
                            }
                        }, 500);
                    } else {
                        ToastUtil.showShortToast(activity, "注销失败：" + baseResp.getErrorMessage());
                    }
                } else {
                    ToastUtil.showShortToast(activity, "注销失败");
                }
            }

            @Override
            public void onFailed(int i, Throwable throwable) {
                DQLog.e("注销异常：" + throwable.getMessage());
                ToastUtil.showShortToast(activity, "网络异常，注销失败");
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.imgBack) {
            finish();
        }
    }
}
